<?php
namespace app\Support;

use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Symfony\Component\HttpFoundation\Response as HttpResponse;

class Response {
	private $response;
	private $statusCode = HttpResponse::HTTP_OK;
	private $title = false;

	public function __construct(ResponseFactory $response) {
		$this->response = $response;
	}
	/**
	 *@title 正常数据返回
	 */
	public function withSuccess($resource, $msg = null) {
		if (is_null($msg)) {
			return $this->json($resource);
		}
		return $this->json(array_merge(['msg' => $msg], ['data' => $resource]));
	}
	/**
	 *@title 数据集数据返回
	 */
	public function withCollectionSuccess(ResourceCollection $resource, $msg = null) {
		$additional = $resource->additional ? $resource->additional : [];
		if ($this->title) {
			$resource = array_merge(['title' => $resource->_table, 'data' => $resource]);
		}
		if ($additional) {
			if (is_array($resource)) {
				$resource = array_merge($resource, $additional);
			} else {
				$resource = array_merge(['data' => $resource], $additional);
			}
		}
		if (is_null($msg)) {
			return $this->json($resource);
		}
		return $this->json(array_merge(['msg' => $msg], ['data' => $resource]));
	}

	/**
	 *201 创建成功
	 */
	public function withCreated($resource = null) {
		$this->statusCode = HttpResponse::HTTP_CREATED;

		if (is_null($resource)) {
			return $this->json();
		}
		return $this->json($resource);
	}

	/**
	 *204 不需刷新
	 */
	public function withNoContent() {
		return $this->setStatusCode(
			HttpResponse::HTTP_NO_CONTENT
		)->json();
	}

	/**
	 *400 参数错误
	 */
	public function withBadRequest($message = 'Bad Request') {
		return $this->setStatusCode(
			HttpResponse::HTTP_BAD_REQUEST
		)->withError($message);
	}

	/**
	 *401 未授权
	 */
	public function withUnauthorized($message = 'Unauthorized') {
		return $this->setStatusCode(
			HttpResponse::HTTP_UNAUTHORIZED
		)->withError($message);
	}

	/**
	 *403
	 */
	public function withForbidden($message = 'Forbidden') {
		return $this->setStatusCode(
			HttpResponse::HTTP_FORBIDDEN
		)->withError($message);
	}

	/**
	 *404 页面未找到
	 */
	public function withNotFound($message = 'Not Found') {
		return $this->setStatusCode(
			HttpResponse::HTTP_NOT_FOUND
		)->withError($message);
	}

	/**
	 * 429 时间段请求时间次数过多
	 */
	public function withTooManyRequests($message = 'Too Many Requests') {
		return $this->setStatusCode(
			HttpResponse::HTTP_TOO_MANY_REQUESTS
		)->withError($message);
	}

	/**
	 * 500 服务器内部错误
	 */
	public function withInternalServer($message = 'Internal Server Error') {
		return $this->setStatusCode(
			HttpResponse::HTTP_INTERNAL_SERVER_ERROR
		)->withError($message);
	}

	public function withError($message) {
		return $this->json([
			'messages' => is_array($message) ? $message : [$message],
		]);
	}

	public function withTitle() {
		$this->title = true;
		return $this;
	}

	public function json($data = [], array $headers = []) {
		return $this->response->json($data, $this->statusCode, $headers);
	}

	public function setStatusCode($statusCode) {
		$this->statusCode = $statusCode;
		return $this;
	}
}